<?php

namespace addons\cms\controller\wxapp;

use addons\cms\model\Comment;
use addons\cms\model\Archives_user;
use addons\cms\model\Msg;
use addons\cms\model\Page;

/**
 * 我的
 */
class My extends Base
{

    protected $noNeedLogin = ['aboutus'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 我发表的评论
     */
    public function comment()
    {
        $page = (int)$this->request->request('page');
        $commentList = Comment::
            with('archives')
            ->where(['user_id' => $this->auth->id])
            ->order('id desc')
            ->page($page, 10)
            ->select();
        foreach ($commentList as $index => $item) {
            $item->create_date = human_date($item->createtime);
        }

        $this->success('', ['commentList' => $commentList]);
    }

    /**
     * 我的收藏
     */
    public function collection()
    {
        $page = (int)$this->request->request('page');
        $collectionList = Archives_user::
        with('archives')
            ->with('user')
            ->where(['user_id' => $this->auth->id])
            ->order('createtime','desc')
            ->page($page, 10)
            ->select();
        $this->success('', ['commentList' => $collectionList]);
    }

    /**
     * 我的消息
     */
    public function getMsg()
    {
        $page = (int)$this->request->request('page');
        $msgList = Msg::with('user')
            ->where(['receive_id' => $this->auth->id])
            ->order('createtime','desc')
            ->page($page, 10)
            ->select();
        $msgList = collection($msgList)->toarray();
        //处理
        foreach ($msgList as $k=>$v){
            $type_name = '';
            switch ($v['type'])
            {
                case 1:
                    $type_name = '评论';break;
                case 2:
                    $type_name = '关注';break;
                case 3:
                    $type_name = '收藏';break;
                case 4:
                    $type_name = '点赞';break;
                case 5:
                    $type_name = '投诉';break;
                case 6:
                    $type_name = '举报';break;
            }
            $msgList[$k]['type_name'] = $type_name;
            $msgList[$k]['date'] = date("Y-m-d",$v['createtime']);
            if(substr($v['user']['avatar'],0,5) != 'https'){
                $avatar = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$v['user']['avatar'];
                $msgList[$k]['user']['avatars'] = $avatar;
            }else{
                $msgList[$k]['user']['avatars'] = $v['user']['avatar'];;
            }
        }
        $this->success('', ['msgList' => $msgList]);
    }

    /**
     * 关于我们
     */
    public function aboutus()
    {
        $pageInfo = Page::getByDiyname('aboutus');
        if (!$pageInfo || $pageInfo['status'] != 'normal') {
            $this->error(__('单页未找到'));
        }
        $this->success('', ['pageInfo' => $pageInfo]);
    }

    /**
     * 免责声明
     */
    public function mian()
    {
        $pageInfo = Page::getByDiyname('mian');
        if (!$pageInfo || $pageInfo['status'] != 'normal') {
            $this->error(__('单页未找到'));
        }
        $this->success('', ['pageInfo' => $pageInfo]);
    }
}
